package lapsnake.util;

public class Funky
{	
	/**Search the source for unescaped occurrences of a special string, e.g. "\n"(newline character) or "\""(double quote character), 
	 * and replace it with a replacement string, e.g. "n"(the letter n) or "\""(double quote character),
	 * and in addition, if the string is not already preceded by an odd number of backslashes, prepend a backslash to the replacement string.<br><br>
	 * For example, if replacing double quotes with double quotes, both a\"b and a"b would become a\"b.<br> <br>
	 * Another example, replacing newline characters with n: 
	 * <pre>a
	 *b</pre>
	 * becomes a\nb<br><br>*/
	public static String replaceAndEscape(String source, String unescaped, String replacementText)
	{
		StringBuffer dstBuf = new StringBuffer(source);
		int i = dstBuf.length()-1;
		while(i>=unescaped.length()-1)
		{
			if(dstBuf.substring(i+1-unescaped.length(), i+1).equals(unescaped))
			{
				//Located an instance of the unescaped string
				//Need to replace it with the replacement string
				dstBuf.replace(i+1-unescaped.length(), i+1, replacementText);
				
				//Need to check if it has been escaped
				int j = i-unescaped.length();
				int escapeCount = 0;
				while(j > 0)
				{
					char cc = dstBuf.charAt(j);
					if(cc == '\\')
						escapeCount++;
					else
						break;
					j--;
				}
				//escapeCount is now the number of escape characters before the unescaped version.
				//If this is an uneven number, then it has already been escaped and nothing needs to be done for this subsequence.
				if(escapeCount%2 == 1)
				{
					i = j;
					continue;
				}
				else
				{
					//Insert an additional escape character to escape the sequence.
					dstBuf.insert(j+1, '\\');
					i = j;
					continue;
				}
			}
			else
			{
				i--;
				continue;
			}
			
		}
		return dstBuf.toString();
	}
	/**Returns a string with the number of whitespaces indicated by the argument.*/
	public static String getIndentString(int spaces)
	{
		return new String(new char[spaces]).replaceAll("\0", " ");
	}
	/**Uses Python 2 rules to convert tabs to spaces on a single line.
	 * <p>
	 *... tabs are replaced (from left to right) by one to eight spaces such 
	  *that the total number of characters up to and including the replacement is 
	  *a multiple of eight (this is intended to be the same rule as used by Unix). 
	  *The total number of spaces preceding the first non-blank character then 
	  *determines the line’s indentation.
	    * </p>*/
	public static String tabsToSpaces(String indentation)
	{
		int spaceCount = 0;
		StringBuilder dst = new StringBuilder();
		int i = 0;
		while(i<indentation.length())
		{
			char c = indentation.charAt(i);
			if(c == '\r')
			{
				if(indentation.charAt(i+1) != '\n' && i != 0)
					System.err.println("Warning: Stray carriage return character in string: \""+indentation+"\"");
				dst.setLength(0);
				spaceCount = 0;
			}
			else if(c == ' ')
			{
				dst.append(' ');
				spaceCount = (spaceCount+1)%8;
			}
			else if(c == '\t')
			{
				dst.append(new String(new char[8-spaceCount]).replaceAll("\0", " "));
				spaceCount = 0;
			}
			else
			{
				dst.append(indentation.substring(i));
				break;
			}
			i++;
		}
		return dst.toString();
	}
}
